//
//  OEOrderDetailViewController.m
//  OneEightyNine
//
//  Created by apple on 2019/3/9.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEOrderDetailViewController.h"


#import "OEOrderHeaderView.h"
#import "OEOrderTableViewCell.h"
#import "OEOrderFooterView.h"

@interface OEOrderDetailViewController ()
@property(nonatomic, strong) OEOrderModel                     *detailModel;/**< <##> */
@end

@implementation OEOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = OESTRING(@"ORDER DETAILS");
    [self.tableView registerClass:OEOrderTableViewCell.class forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:OEOrderHeaderView.class forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableView registerClass:OEOrderFooterView.class forHeaderFooterViewReuseIdentifier:@"footer"];
    
    [self updateData];
    // Do any additional setup after loading the view.
}
- (void)updateData{
    [OEOrderDataController sendOrderDetailRequestWithOrder_id:self.order_id handle:^(BOOL success, NSString * _Nonnull errorString, NSDictionary * _Nonnull resp) {
        if (success) {
             OEOrderModel * model = [OEOrderModel mj_objectWithKeyValues:resp];
            model.cus_isDetailVC = YES;
            self.detailModel = model;
           
            [self configSubView];
            [self.tableView reloadData];
        }
    }];
}
#pragma mark --configSubView
- (void)configSubView{
    [self configTableHeader];
    [self configTableFooter];
}
- (void)configTableHeader{
    UIView * header = [[UIView alloc] initWithFrame:UIScreenFrame];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    imageView.image = [UIImage imageNamed:@"750×220状态"];
    [header addSubview:imageView];
    
    STLabel * statusLabel = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth, 60)
                                                    text:self.detailModel.show_status
                                               textColor:UIColor.whiteColor
                                                    font:22
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    [header addSubview:statusLabel];
    
    UIImageView * addimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"30×30地址"]];
    addimageView.left = 15;
    addimageView.top = imageView.bottom + 15;
    [header addSubview:addimageView];
    
    STLabel * consneLabel = [[STLabel alloc] initWithFrame:CGRectMake(addimageView.right + 10, 0, UIScreenWidth - addimageView.right - 20, 25)
                                                      text:[NSString stringWithFormat:@"%@  %@",self.detailModel.consignee,self.detailModel.mobile]
                                                 textColor:FirstTextColor
                                                      font:15
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [header addSubview:consneLabel];
    consneLabel.centerY = addimageView.centerY;
    
    STLabel * addLabel = [[STLabel alloc] initWithFrame:CGRectMake(addimageView.right + 10, consneLabel.bottom, consneLabel.width, 40)
                                                      text:[NSString stringWithFormat:@"%@  ",self.detailModel.supplier_info.address]
                                                 textColor:SecendTextColor
                                                      font:13
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [header addSubview:addLabel];
    
    header.height = addLabel.bottom + 10;
    self.tableView.tableHeaderView = header;
}
- (void)configTableFooter{
    UIView * footer = [[UIView alloc] initWithFrame:UIScreenFrame];
    STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 60)
                                                   text:@"ORDER INFORMATION"
                                              textColor:UIColor.blackColor
                                                   font:22
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentLeft];
    [footer addSubview:titleLabel];
    CGFloat top = titleLabel.bottom + 30;
    ({
        if (self.detailModel.order_sn.length) {
            STLabel * lable = [self lableWithLeftTitle:@"Order number" rightTitle:self.detailModel.order_sn];
            [footer addSubview:lable];
            lable.top = top;
            top = lable.bottom + 10;
        }
    });
    

    
    ({
        if (self.detailModel.add_time.length) {
            NSDate * date = [NSDate dateWithTimeIntervalSince1970:self.detailModel.add_time.integerValue];
            STLabel * lable = [self lableWithLeftTitle:@"Order Time" rightTitle:date.st_yyyyMMddhhmmssWithDate];
            [footer addSubview:lable];
            lable.top = top;
            top = lable.bottom + 10;
        }
    });
    
    ({
        if (self.detailModel.format_pay_time.length) {
            STLabel * lable = [self lableWithLeftTitle:@"Order Time" rightTitle:self.detailModel.format_pay_time];
            [footer addSubview:lable];
            lable.top = top;
            top = lable.bottom + 10;
        }
    });
    
    ({
        if (self.detailModel.affirm_received_time.length) {
            NSDate * date = [NSDate dateWithTimeIntervalSince1970:self.detailModel.affirm_received_time.integerValue];
            STLabel * lable = [self lableWithLeftTitle:@"Affirm received time" rightTitle:date.st_yyyyMMddhhmmssWithDate];
            [footer addSubview:lable];
            lable.top = top;
            top = lable.bottom + 10;
        }
    });
    
    footer.height = top + 20;
    self.tableView.tableFooterView = footer;
}
- (STLabel*)lableWithLeftTitle:(NSString*)leftTitle rightTitle:(NSString*)rightTitle{
    STLabel * titleLabel = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 44)
                                                     text:[NSString stringWithFormat:@"%@:%@",leftTitle,rightTitle]
                                               textColor:FirstTextColor
                                                    font:15
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    titleLabel.attributedText = [titleLabel.text st_convertAttributeStringWithKeyWord:rightTitle attributes:@{NSForegroundColorAttributeName:SecendTextColor}];
    [NSString lableAutoAdjustheightWithLabel:titleLabel];
    return titleLabel;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (!self.detailModel) {
        return NO;
    }
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.detailModel.goods_list.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return OEOrderTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return OEOrderHeaderView.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return OEOrderFooterView.cellHeight;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    OEOrderHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    OEOrderModel * model = _detailModel;
    header.model = model;
    [header setOnSelctedHeaderHandle:^{
        [TMUtils gotoShopHeaderWithModel:model.supplier_info];
    }];
    return header;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    OEOrderFooterView * footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer"];
    OEOrderModel * model = _detailModel;
    footer.model = model;
    __weak typeof(self) weakSelf =  self;
    [footer setOnSelctedHandle:^(NSString * _Nonnull title) {
        [weakSelf dealBootomActionWithTitle:title order_id:model.order_id orderModel:model];
    }];
    return footer;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OEOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    OEOrderModel * model = _detailModel;
    OEGoodModel * goodModel = model.goods_list[indexPath.row];
    cell.model = goodModel;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)dealBootomActionWithTitle:(NSString*)title order_id:(NSString*)order_id orderModel:(OEOrderModel*)orderModel{
    if ([title isEqualToString:@"CANCLE"]) {
        
        [self st_showAlertTitle:@"Are you sure you want to cancle the order？" message:@"" leftTitle:@"NO" rightTitle:@"CANCLE" block:^(NSString *name) {
            if ([name isEqualToString:@"CANCLE"]) {
                [OEOrderDataController sendCancleOrderRequestWithOrder_id:order_id handle:^(BOOL success, NSString * _Nonnull errorString, NSDictionary * _Nonnull resp) {
                    if (success) {
                       [self updateData];
                    }
                }];
            }
        }];
    }
    if ([title isEqualToString:@"RETURN"]) {
        [self st_showAlertTitle:@"Are you sure you want to return it? After the merchant confirms, you will return successfully" message:@"" leftTitle:@"CANCLE" rightTitle:@"RETURN" block:^(NSString *name) {
            if ([name isEqualToString:@"RETURN"]) {
                [OEOrderDataController sendReturnOrderRequestWithOrder_id:order_id handle:^(BOOL success, NSString * _Nonnull errorString, NSDictionary * _Nonnull resp) {
                    if (success) {
                        [self updateData];
                    }
                }];
            }
        }];
        
    }
    if ([title isEqualToString:@"DELETE"]) {
        [self st_showAlertTitle:@"Are you sure you want to delete the order？" message:@"" leftTitle:@"CANCLE" rightTitle:@"DELETE" block:^(NSString *name) {
            if ([name isEqualToString:@"DELETE"]) {
                [OEOrderDataController sendDeleteOrderRequestWithOrder_id:order_id handle:^(BOOL success, NSString * _Nonnull errorString, NSDictionary * _Nonnull resp) {
                    if (success) {
                        [self updateData];
                    }
                }];
            }
        }];
    }
    
    if ([title isEqualToString:@"BUY AGAIN"]) {
        //去店铺
        OEShopEasyModel * model =  OEShopEasyModel.new;
        model.supplier_id = orderModel.supplier_id;
        [TMUtils gotoShopHeaderWithModel:model];
    }

}

@end
